import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '@/views/LoginView.vue'
import RegisterView from '@/views/RegisterView.vue'
import HomeView from '@/views/HomeView.vue'
import RecommendForum from '@/views/RecommendForum.vue'
import UserInfo from '@/views/UserInfo.vue'
import CommentPost from '@/views/CommentPost.vue'
import PublishUpdates from '@/views/PublishUpdates.vue'
import PublishArticle from '@/views/PublishArticle.vue'
import Drafts from '@/views/Drafts.vue'




const routes = [
    // 登录
    {
        path: '/login',
        name: 'Login',
        component: LoginView,
        meta: { title: "登录", type: "front" },
    },
    // 注册
    {
        path: '/register',
        name: 'Register',
        component: RegisterView,
        meta: { title: "注册", type: "front" },
    },
    // 首页
    {
        path: '/',
        name: 'Home',
        component: HomeView,
        meta: { title: "云南三益文化国防基金会", type: "admin" },
    },
    // 论坛推荐
    {
        path: '/recommendForm',
        name: 'RecommendForm',
        component: RecommendForum,
        meta: { title: "论坛推荐", type: "admin" },
    },
    // 版块帖子
    {
        path: '/PartPost',
        name: 'PartPost',
        component: () => import('@/views/PartPost.vue'), // 使用懒加载,
        meta: { title: "版块帖子", type: "admin" },
    },
    // 博主个人主页
    {
        path: '/userInfo',
        name: 'UserInfo',
        component: UserInfo,
        meta: { title: "个人中心", type: "admin" },
    },
    // 查看某个用户个人主页
    {
        path: '/otherInfo',
        name: 'OtherInfo',
        component: () => import('@/views/OtherInfo.vue'), // 使用懒加载,
        meta: { title: "TA的个人中心", type: "admin" },
    },
    // 博主列表
    {
        path: '/bloggerList',
        name: 'BloggerList',
        component: () => import('@/views/BloggerList.vue'), // 使用懒加载,
        meta: { title: "博主列表", type: "admin" },
    },
    // 帖子评论
    {
        path: '/commentPost',
        name: 'CommentPost',
        component: CommentPost,
        meta: { title: "帖子详情", type: "admin" },
    },
    // 发布动态
    {
        path: '/publishUpdates',
        name: 'PublishUpdates',
        component: PublishUpdates,
        meta: { title: "发布动态", type: "admin" },
    },
    // 发布长文
    {
        path: '/publishArticle',
        name: 'PublishArticle',
        component: PublishArticle,
        meta: { title: "发布长文", type: "admin" },
    },
    // 草稿箱
    {
        path: '/drafts',
        name: 'Drafts',
        component: Drafts,
        meta: { title: "草稿箱", type: "admin" },
    },
    {
        path: '/search',
        name: 'Search',
        component: () => import('@/views/Search.vue'), // 使用懒加载,
        meta: { title: "搜索结果", type: "admin" },
    },
    {
        path: '/message',
        name: 'Message',
        component: () => import('@/views/Message.vue'), // 使用懒加载,
        meta: { title: "消息通知", type: "admin" },
    },
    // 404
    {
        path: '/:pathMatch(.*)*', // 捕获所有路由
        name: 'NotFound',
        component: () => import('@/views/NotFound.vue')
    }
    // 其他路由配置...
];

const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { top: 0 }
        }
    }
})

// 路由守卫
router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    // 判断token是否存在
    let token = sessionStorage.getItem('token');
    if (token) {
        next()
    } else {
        if (to.meta.type == 'front') {
            next()
        } else {
            // 未登录跳转登录
            next('/login')
        }

    }
})

export default router;
